package de.lgohlke.helper;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.bouncycastle.util.encoders.Hex;

/**
 * <p>CryptHelper class.</p>
 *
 * @author lars
 * @version $Id: $
 */
public class CryptHelper
{
  // public static String convertSecretKeyToHex(final Key secretKey)
  // {
  // return new String(convertBytesToHex(secretKey.getEncoded()));
  // }

  // protected static byte[] convertHexToBytes(final char[] bytes)
  // {
  // return (bytes.length == 0) ? null : org.jsecurity.codec.Hex.decode(bytes);
  // }

  // protected static byte[] convertHexToBytes(final String str)
  // {
  // return convertHexToBytes(str == null ? "".toCharArray() : str.toCharArray());
  // }
  // protected static String convertBytesToHex(final String input)
  // {
  // return convertBytesToHex(input.getBytes());
  // }

  /**
   * <p>convertBytesToHex.</p>
   *
   * @param bytes an array of byte.
   * @return a {@link java.lang.String} object.
   */
  protected static String convertBytesToHex(final byte[] bytes)
  {
    return new String(((bytes == null) || (bytes.length == 0)) ? null : Hex.encode(bytes));
  }

  /**
   * <p>hashMD5.</p>
   *
   * @param input a {@link java.lang.String} object.
   * @return a {@link java.lang.String} object.
   * @throws java.security.NoSuchAlgorithmException if any.
   */
  public static String hashMD5(final String input) throws NoSuchAlgorithmException
  {
    return hashMD5(input.getBytes());
  }

  /**
   * <p>hashMD5.</p>
   *
   * @param input an array of byte.
   * @return a {@link java.lang.String} object.
   * @throws java.security.NoSuchAlgorithmException if any.
   */
  public static String hashMD5(final byte[] input) throws NoSuchAlgorithmException
  {
    MessageDigest md = MessageDigest.getInstance("MD5");
    md.reset();
    md.update(input);
    // md.update(salt.getBytes());
    byte[] bytes = md.digest();

    return convertBytesToHex(bytes);
  }
}
